<?php
namespace App\Extensions;

use Encore\Admin\Admin;
use Encore\Admin\Grid\Tools\AbstractTool;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Request;

class OrderGender extends AbstractTool
{
    private $model;

    function __construct($model)
    {
        $this->model = $model;
    }

    protected function script()
    {
        $token = csrf_token();
        $url=url('admin/common/sort');
        $model=urlencode($this->model);
        return <<<EOT
 $(function () {
            var tbody = $("tbody");
            tbody.sortable();
            tbody.sortable({
                update: function () {
                    var id = [];
                    $("tr td:nth-child(2)").each(function () {
                        id.push($(this).html());
                    });
                    $.ajax({
                        headers: {
                            'X-CSRF-TOKEN': '$token'
                        },
                        url: '$url',
                        type: 'post',
                        data: {
                            id: id,
                            model: '$model'
                        }
                    });
                }
            });
        });
EOT;
    }

    public function render()
    {

        Admin::script($this->script());
//        Admin::js('plugins/sortable.js');
    }
}